package com.gensdai.leliang.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.bean.OffLineStoreBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/1.
 */

public class StoreListRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private LayoutInflater mLayoutInflater = null;
    private Context context;

    public List<OffLineStoreBean> data = new ArrayList<>();

    OnRecyclerViewItemClick click = null;


    public void addList(List<OffLineStoreBean> list){
        if(data != null){
            data.addAll(list);
        }

        notifyDataSetChanged();
    }

    public StoreListRecyclerAdapter(Context mContext, List<OffLineStoreBean> data) {
        this.context = mContext;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.data = data;

    }

    public StoreListRecyclerAdapter(Context context) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setOnItemClick(OnRecyclerViewItemClick onclick) {
        click = onclick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContentViewHolder(mLayoutInflater.inflate(
                R.layout.item_home_offline_store, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final OffLineStoreBean bean = data.get(position);

        ((ContentViewHolder)holder).rootview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(click != null){
                    click.OnItemClick(bean);
                }
            }
        });
        ((ContentViewHolder)holder).indetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(click != null){
                    click.OnItemClick(bean);
                }
            }
        });



        ((ContentViewHolder)holder).title.setText(bean.shopName.isEmpty()?"":bean.shopName);
        Uri uri = Uri.parse(bean.thumPic.isEmpty()?"":bean.thumPic);
        ((ContentViewHolder)holder).icon.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.icon)
        SimpleDraweeView icon;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.rootview)
        LinearLayout rootview;
        @BindView(R.id.indetail)
        Button indetail;

        ContentViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnRecyclerViewItemClick {
        void OnItemClick(OffLineStoreBean pos);
    }
}
